package com.fpt.edu.vn.h2tv.service;

import java.util.ArrayList;
import java.util.List;

import com.fpt.edu.vn.h2tv.common.ElementName;
import com.fpt.edu.vn.h2tv.dao.EntityManagerHelper;
import com.fpt.edu.vn.h2tv.dao.ITbAccountDAO;
import com.fpt.edu.vn.h2tv.dao.TbAccountDAO;
import com.fpt.edu.vn.h2tv.dao.entity.TbAccount;
import com.fpt.edu.vn.h2tv.dto.UserInfo;
import com.fpt.edu.vn.h2tv.dto.UserResponse;

public class UserService implements IBaseService {

    @Override
    public Object getResponse() {
        // TODO Auto-generated method stub
        return getUserResponse();
    }

    private UserResponse getUserResponse() {
        ITbAccountDAO accDAO = new TbAccountDAO();
        // find all user on DB
        List<TbAccount> tbAccountList = accDAO.findAll();
        UserResponse response = new UserResponse();
        List<UserInfo> userList = new ArrayList<UserInfo>();
        if (tbAccountList != null && tbAccountList.size() > 0) {
            for (TbAccount account : tbAccountList) {
                UserInfo userDTO = new UserInfo();
                userDTO.setUserId(account.getUserid().longValue());
                userDTO.setUserName(account.getUsername());
                userDTO.setPwd(account.getPassword());
                userDTO.setFirstName(account.getFirstname());
                userDTO.setLastName(account.getLastname());
                userDTO.setAddress(account.getAddress());
                userDTO.setPhone(account.getPhonenumber());
                userDTO.setRole(account.getRole());
                userList.add(userDTO);
            }
            response.setStatus(ElementName.SUCCESS);
        } else {
            response.setStatus(ElementName.NO_DATA);
        }

        response.setUsers(userList);
        return response;
    }

    public static UserInfo registerUser(String userName, String password, String email, String firstName,
            String lastName, String phone, String address) {
        TbAccountDAO dao = new TbAccountDAO();
        TbAccount newAccount = new TbAccount();
        newAccount.setAddress(address);
        newAccount.setAvatar("img\\default-avatar.jpg");
        newAccount.setFirstname(firstName);
        newAccount.setIsBanned(false);
        newAccount.setLastname(lastName);
        newAccount.setPassword(password);
        newAccount.setPhonenumber(phone);
        newAccount.setRole(ElementName.CUSTOMER);
        newAccount.setUsername(userName);

        EntityManagerHelper.beginTransaction();
        TbAccount result = dao.update(newAccount);
        EntityManagerHelper.commit();

        UserInfo info = new UserInfo();
        info.setUserName(result.getUsername());
        info.setRole(result.getRole());
        info.setUserId(result.getUserid());
        info.setAvatar(result.getAvatar());
        info.setEmail(result.getEmail());
        return info;
    }

    public static UserResponse getAllStaff() {
        ITbAccountDAO accountDAO = new TbAccountDAO();
        List<TbAccount> tbAccountsList = accountDAO.findStaff();
        UserResponse response = new UserResponse();
        List<UserInfo> accountList = new ArrayList<UserInfo>();
        if (tbAccountsList != null && tbAccountsList.size() > 0) {
            for (TbAccount account : tbAccountsList) {
                UserInfo accountDTO = new UserInfo();
                accountDTO.setUserId(account.getUserid().longValue());
                accountDTO.setUserName(account.getUsername());
                accountDTO.setPwd(account.getPassword());
                accountDTO.setAvatar(account.getAvatar());
                accountDTO.setLastName(account.getLastname());
                accountDTO.setFirstName(account.getFirstname());
                accountDTO.setPhone(account.getPhonenumber());
                accountDTO.setAddress(account.getAddress());
                accountDTO.setRole(account.getRole());
                accountDTO.setIsBanned(account.getIsBanned().booleanValue());
                accountDTO.setEmail(account.getEmail());
                accountList.add(accountDTO);
            }
            response.setStatus(ElementName.SUCCESS);
        } else {
            response.setStatus(ElementName.NO_DATA);
        }
        response.setUsers(accountList);
        return response;
    }

    public static UserResponse getAllCustomer() {
        ITbAccountDAO accountDAO = new TbAccountDAO();
        List<TbAccount> tbAccountsList = accountDAO.findCustomer();
        UserResponse response = new UserResponse();
        List<UserInfo> accountList = new ArrayList<UserInfo>();
        if (tbAccountsList != null && tbAccountsList.size() > 0) {
            for (TbAccount account : tbAccountsList) {
                UserInfo accountDTO = new UserInfo();
                accountDTO.setUserId(account.getUserid().longValue());
                accountDTO.setUserName(account.getUsername());
                accountDTO.setPwd(account.getPassword());
                accountDTO.setAvatar(account.getAvatar());
                accountDTO.setLastName(account.getLastname());
                accountDTO.setFirstName(account.getFirstname());
                accountDTO.setPhone(account.getPhonenumber());
                accountDTO.setAddress(account.getAddress());
                accountDTO.setRole(account.getRole());
                accountDTO.setIsBanned(account.getIsBanned().booleanValue());
                accountDTO.setEmail(account.getEmail());
                accountList.add(accountDTO);
            }
            response.setStatus(ElementName.SUCCESS);
        } else {
            response.setStatus(ElementName.NO_DATA);
        }
        response.setUsers(accountList);
        return response;
    }

    public static String addStaff(String userName, String password, String firstName, String lastName, String phone,
            String address, String role, String email) {
        TbAccountDAO dao = new TbAccountDAO();
        TbAccount newAccount = new TbAccount();
        newAccount.setAddress(address);
        newAccount.setEmail(email);
        newAccount.setFirstname(firstName);
        newAccount.setIsBanned(false);
        newAccount.setLastname(lastName);
        newAccount.setPassword(password);
        newAccount.setPhonenumber(phone);
        newAccount.setRole(role);
//        if (role.equals(ElementName.ADMIN)) {
//            newAccount.setAvatar("admin.png");
//        } else if (role.equals(ElementName.STAFF)) {
//            newAccount.setAvatar("staff.png");
//        } else {
//            newAccount.setAvatar("null");
//        }
        newAccount.setUsername(userName);

        EntityManagerHelper.beginTransaction();
        dao.save(newAccount);
        EntityManagerHelper.commit();

        String success = "Add new staff successful!";
        return success;
    }

    public static String deleteAccount(Long id) {
        ITbAccountDAO accountDAO = new TbAccountDAO();
        TbAccount account = accountDAO.findById(id);

        EntityManagerHelper.beginTransaction();
        accountDAO.delete(account);
        EntityManagerHelper.commit();

        String success = "delete user " + id + " successful!";
        return success;
    }

    public static String updateStaff(Long id, String lName, String fName, String phone, String address, String role,
            Boolean isBanned, String email) {
        String noti;
        ITbAccountDAO accountDAO = new TbAccountDAO();
        TbAccount account = accountDAO.findById(id);
        if (account != null) {
            account.setAddress(address);
            account.setLastname(lName);
            account.setFirstname(fName);
            account.setIsBanned(isBanned);
            account.setPhonenumber(phone);
            account.setRole(role);
//            if (role.equals(ElementName.ADMIN)) {
//                account.setAvatar("admin.png");
//            } else if (role.equals(ElementName.STAFF)) {
//                account.setAvatar("staff.png");
//            } else {
//                account.setAvatar("null");
//            }
            account.setEmail(email);
            EntityManagerHelper.beginTransaction();
            accountDAO.update(account);
            EntityManagerHelper.commit();

            noti = "update user " + id + " successful!";
        } else {
            noti = "update user " + id + " failed!";
        }

        return noti;
    }

    public static String updateCus(Long id, Boolean isBanned) {
        String noti;
        ITbAccountDAO accountDAO = new TbAccountDAO();
        TbAccount account = accountDAO.findById(id);
        
        if (account != null) {
            account.setIsBanned(isBanned);

            EntityManagerHelper.beginTransaction();
            accountDAO.update(account);
            EntityManagerHelper.commit();
            noti = "update customer " + id + " successful!";
        } else {
            noti = "update customer " + id + " failed!";
        }

        return noti;
    }

    public static UserResponse getAllAdmin() {
        ITbAccountDAO accountDAO = new TbAccountDAO();
        List<TbAccount> tbAccountsList = accountDAO.findAllAdmin();
        UserResponse response = new UserResponse();
        List<UserInfo> accountList = new ArrayList<UserInfo>();
        if (tbAccountsList != null && tbAccountsList.size() > 0) {
            for (TbAccount account : tbAccountsList) {
                UserInfo accountDTO = new UserInfo();
                accountDTO.setUserId(account.getUserid().longValue());
                accountDTO.setUserName(account.getUsername());
                accountDTO.setPwd(account.getPassword());
                accountDTO.setAvatar(account.getAvatar());
                accountDTO.setLastName(account.getLastname());
                accountDTO.setFirstName(account.getFirstname());
                accountDTO.setPhone(account.getPhonenumber());
                accountDTO.setAddress(account.getAddress());
                accountDTO.setRole(account.getRole());
                accountDTO.setIsBanned(account.getIsBanned().booleanValue());
                accountDTO.setEmail(account.getEmail());
                accountList.add(accountDTO);
            }
            response.setStatus(ElementName.SUCCESS);
        } else {
            response.setStatus(ElementName.NO_DATA);
        }
        response.setUsers(accountList);
        return response;
    }

    public static UserResponse getStaff() {
        ITbAccountDAO accountDAO = new TbAccountDAO();
        List<TbAccount> tbAccountsList = accountDAO.findAllStaff();
        UserResponse response = new UserResponse();
        List<UserInfo> accountList = new ArrayList<UserInfo>();
        if (tbAccountsList != null && tbAccountsList.size() > 0) {
            for (TbAccount account : tbAccountsList) {
                UserInfo accountDTO = new UserInfo();
                accountDTO.setUserId(account.getUserid().longValue());
                accountDTO.setUserName(account.getUsername());
                accountDTO.setPwd(account.getPassword());
                accountDTO.setAvatar(account.getAvatar());
                accountDTO.setLastName(account.getLastname());
                accountDTO.setFirstName(account.getFirstname());
                accountDTO.setPhone(account.getPhonenumber());
                accountDTO.setAddress(account.getAddress());
                accountDTO.setRole(account.getRole());
                accountDTO.setIsBanned(account.getIsBanned().booleanValue());
                accountDTO.setEmail(account.getEmail());
                accountList.add(accountDTO);
            }
            response.setStatus(ElementName.SUCCESS);
        } else {
            response.setStatus(ElementName.NO_DATA);
        }
        response.setUsers(accountList);
        return response;
    }

    public static UserResponse searchStaff(String username) {
        ITbAccountDAO accountDAO = new TbAccountDAO();
        List<TbAccount> tbAccountsList = accountDAO.searchStaffByUsername(username);
        UserResponse response = new UserResponse();
        List<UserInfo> accountList = new ArrayList<UserInfo>();
        if (tbAccountsList != null && tbAccountsList.size() > 0) {
            for (TbAccount account : tbAccountsList) {
                UserInfo accountDTO = new UserInfo();
                accountDTO.setUserId(account.getUserid().longValue());
                accountDTO.setUserName(account.getUsername());
                accountDTO.setPwd(account.getPassword());
                accountDTO.setAvatar(account.getAvatar());
                accountDTO.setLastName(account.getLastname());
                accountDTO.setFirstName(account.getFirstname());
                accountDTO.setPhone(account.getPhonenumber());
                accountDTO.setAddress(account.getAddress());
                accountDTO.setRole(account.getRole());
                accountDTO.setIsBanned(account.getIsBanned().booleanValue());
                accountDTO.setEmail(account.getEmail());
                accountList.add(accountDTO);
            }
            response.setStatus(ElementName.SUCCESS);
        } else {
            response.setStatus(ElementName.NO_DATA);
        }
        response.setUsers(accountList);
        return response;
    }
    
    public static UserResponse searchCustomer(String username) {
        ITbAccountDAO accountDAO = new TbAccountDAO();
        List<TbAccount> tbAccountsList = accountDAO.searchCustomerByUsername(username);
        UserResponse response = new UserResponse();
        List<UserInfo> accountList = new ArrayList<UserInfo>();
        if (tbAccountsList != null && tbAccountsList.size() > 0) {
            for (TbAccount account : tbAccountsList) {
                UserInfo accountDTO = new UserInfo();
                accountDTO.setUserId(account.getUserid().longValue());
                accountDTO.setUserName(account.getUsername());
                accountDTO.setPwd(account.getPassword());
                accountDTO.setAvatar(account.getAvatar());
                accountDTO.setLastName(account.getLastname());
                accountDTO.setFirstName(account.getFirstname());
                accountDTO.setPhone(account.getPhonenumber());
                accountDTO.setAddress(account.getAddress());
                accountDTO.setRole(account.getRole());
                accountDTO.setIsBanned(account.getIsBanned().booleanValue());
                accountDTO.setEmail(account.getEmail());
                accountList.add(accountDTO);
            }
            response.setStatus(ElementName.SUCCESS);
        } else {
            response.setStatus(ElementName.NO_DATA);
        }
        response.setUsers(accountList);
        return response;
    }

}
